﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using WebTech.Coursework02.Models;

namespace WebTech.Coursework02.Controllers
{
    public class DepartmentController : Controller
    {
        private ExtractionEntities db = new ExtractionEntities();

        //
        // GET: /Department/

        public ActionResult Index()
        {
            return View(db.departments.ToList());
        }

        //
        // GET: /Department/Details/5

        public ActionResult Details(string id = null)
        {
            department department = db.departments.Find(id);
            if (department == null)
            {
                return HttpNotFound();
            }
            return View(department);
        }

        //
        // GET: /Department/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Department/Create

        [HttpPost]
        public ActionResult Create(department department)
        {
            if (ModelState.IsValid)
            {
                db.departments.Add(department);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(department);
        }

        //
        // GET: /Department/Edit/5

        public ActionResult Edit(string id = null)
        {
            department department = db.departments.Find(id);
            if (department == null)
            {
                return HttpNotFound();
            }
            return View(department);
        }

        //
        // POST: /Department/Edit/5

        [HttpPost]
        public ActionResult Edit(department department)
        {
            if (ModelState.IsValid)
            {
                db.Entry(department).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(department);
        }

        //
        // GET: /Department/Delete/5

        public ActionResult Delete(string id = null)
        {
            department department = db.departments.Find(id);
            if (department == null)
            {
                return HttpNotFound();
            }
            return View(department);
        }

        //
        // POST: /Department/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            department department = db.departments.Find(id);
            db.departments.Remove(department);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        
        public JsonResult GetDepartments()
        {
            System.Func<department, GridDepartment> secondarySelector = x => new GridDepartment() { deptName = x.dept_name, deptNo = x.dept_no}; 
            IEnumerable<GridDepartment> departments =  db.departments.AsEnumerable().Select(secondarySelector);
           
            return this.Json(new Response(departments.AsQueryable().ToArray(), departments.Count()), JsonRequestBehavior.AllowGet);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}